package com.bqtv.sport.commen.utils

import android.content.Context
import java.io.File


class FileUtils {
    companion object{
        fun getCacheSize(context: Context): Long {
            return getFolderSize(context.cacheDir)+getFolderSize(context.cacheDir)
        }
        fun getFolderSize(file: File?): Long {
            var size: Long = 0
            if (file != null) {
                if (file.isDirectory) {
                    val files = file.listFiles()
                    for (f in files) {
                        size += getFolderSize(f)
                    }
                } else {
                    size = file.length()
                }
            }
            return size
        }

        fun clearCache(context: Context) {
            val cacheDir: File = context.cacheDir
            deleteDirectory(cacheDir)
        }

        fun deleteDirectory(dir: File?): Boolean {
            if (dir != null && dir.isDirectory) {
                val children = dir.list()
                for (i in children.indices) {
                    val success = deleteDirectory(File(dir, children[i]))
                    if (!success) {
                        return false
                    }
                }
            }
            return dir!!.delete()
        }
    }
}